package com.xinmao.quantitative.trad.strategies.combine;

import com.xinmao.quantitative.trad.strategies.IStrategy;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Rule;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.StochasticOscillatorKIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.helpers.HighestValueIndicator;
import org.ta4j.core.indicators.helpers.LowestValueIndicator;
import org.ta4j.core.indicators.helpers.PreviousValueIndicator;
import org.ta4j.core.indicators.volume.OnBalanceVolumeIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.CrossedDownIndicatorRule;
import org.ta4j.core.rules.CrossedUpIndicatorRule;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.UnderIndicatorRule;

public class StochasticStrategy implements IStrategy {
    @Override
    public Strategy buildStrategy(BarSeries series) {

        PreviousValueIndicator closePrice = new PreviousValueIndicator(new ClosePriceIndicator(series));

        StochasticOscillatorKIndicator stochasticOscillK = new StochasticOscillatorKIndicator(series, 14);

        SMAIndicator sma30 = new SMAIndicator(closePrice, 26);
        OnBalanceVolumeIndicator obv = new OnBalanceVolumeIndicator(series);

        HighestValueIndicator highestPrice =  new HighestValueIndicator(new PreviousValueIndicator(closePrice), 5);
        LowestValueIndicator lowestPrice = new LowestValueIndicator(new PreviousValueIndicator(closePrice), 5);

        Rule entryRule = new OverIndicatorRule(closePrice, highestPrice)
                .and(new CrossedUpIndicatorRule(stochasticOscillK, 55))
                .and(new OverIndicatorRule(obv, sma30));

        Rule exitRule = new UnderIndicatorRule(closePrice, lowestPrice)
                .and(new CrossedDownIndicatorRule(stochasticOscillK, 50));


        return new BaseStrategy("Stochastic",entryRule, exitRule);
    }

    @Override
    public Num getScore() {
        return DecimalNum.valueOf(1);
    }
}
